home *** CD-ROM | disk | FTP | other *** search
- // ==================================================
- // CTouchMeApp.cp
- // Copyright (C) 1996 Mizutori Tetsuya, July 4 1996, August 4, 1996.
- // ==================================================
- // All documents are pretty-printed in Geneva 10-point font.
-
- #include <TextUtils.h>
- //#include <Balloons.h>
-
- #include <LGrowZone.h>
- #include <LDocApplication.h>
- #include <LWindow.h>
- #include <LMenuBar.h>
- #include <LMenu.h>
- #include <LString.h>
-
- #include <PP_Messages.h>
- #include <PP_Resources.h>
- #include <PPobClasses.h>
-
- #include <UDrawingState.h>
- #include <UMemoryMgr.h>
- #include <URegistrar.h>
- #include <UDesktop.h>
- #include <UWindows.h>
-
- #include "touchMeConstants.h"
- #include "touchMeRegistry.h"
- #include "CTouchMeAppleEvents.h"
- #include "CTouchMeApp.h"
- #include "CTouchMePref.h"
- #include "CRadioButton.h"
- #include "CDateEditField.h"
- #include "CTouchMeDialog.h"
- #include "LHelpMenu.h"
- #include "UFileInfo.h"
- #include "UErrorMessage.h"
-
-
- // Patch for the colored alert dialog. Thanks to Rokkaku Fumio.
- #define PATCH_ACTB TRUE
-
- #ifdef PATCH_ACTB
- static pascal Boolean MyAlertPatch( DialogRef dialog, EventRecord *event, short *item );
- ModalFilterUPP AlertPatchUPP;
- #endif // PATCH_ACTB
-
-
- CTouchMePref * gPref = nil;
- CTouchMeDialog * gDialog = nil;
- LHelpMenu * gHelpMenu = nil;
-
-
- // --------------------------------------------------
- // ・ Main Program
- // --------------------------------------------------
-
- void main( void )
- {
- SetDebugThrow_( debugAction_Alert );
- SetDebugSignal_( debugAction_Alert );
-
- InitializeHeap( 3 );
- UQDGlobals::InitializeToolbox( &qd );
-
- new LGrowZone( 20000 );
-
- // Create a preferences class object corresponding to "touchMe Prefs" file.
- Str255 theFilename;
- ::GetIndString( theFilename, rSTRx_TouchMe, rSTRx_TouchMe_PrefFile );
- gPref = new CTouchMePref( theFilename );
-
- // Initialize the preferences reading from preferences file, or by default value.
- gPref->LoadPrefData();
-
- CTouchMeApp theApp;
- theApp.Run();
- }
-
-
- // --------------------------------------------------
- // ・ CTouchMeApp
- // --------------------------------------------------
- // Constructor
-
- CTouchMeApp::CTouchMeApp()
- {
- // Add Help Menu item. My 'LHelpMenu' is a subclass of the standard 'LMenu'.
- Str255 theMenuItemString;
- ::GetIndString( theMenuItemString, rSTRx_TouchMe, rSTRx_TouchMe_HelpMenu );
- gHelpMenu = (LHelpMenu *) new LHelpMenu();
- gHelpMenu->InsertCommand( theMenuItemString, cmd_HelpMenu, 255 /*at the end of menu*/ );
- gHelpMenu->InstallMenu( (LMenuBar *) LMenuBar::GetCurrentMenuBar() );
-
- // Register functions to create core PowerPlant classes.
- // RegisterAllPPClasses();
- RegisterPainClasses();
-
- // Setup my program process status.
- mOpenApplication = false;
- mOpenDocument = false;
- mKeyModifier = false;
- mCountDoc = 0;
-
- #ifdef PATCH_ACTB
- AlertPatchUPP = NewModalFilterProc( MyAlertPatch );
- #endif // PATCH_ACTB
- }
-
-
- // --------------------------------------------------
- // ・ ~CTouchMeApp
- // --------------------------------------------------
- // Destructor
-
- CTouchMeApp::~CTouchMeApp()
- {
- #ifdef PATCH_ACTB
-
- // if ( AlertPatchUPP != nil ) {
- // DisposeRoutineDescriptor( AlertPatchUPP );
- // }
- // AlertPatchUPP = nil;
-
- #endif // PATCH_ACTB
- }
-
-
- // --------------------------------------------------
- // ・ StartUp
- // --------------------------------------------------
-
- void
- CTouchMeApp::StartUp()
- {
- // Setup my program process status.
- mOpenApplication = true;
-
- // Create the preferences dialog.
- gDialog = (CTouchMeDialog *)
- LWindow::CreateWindow( rPPob_TouchMeDialog, this );
- Assert_( gDialog != nil );
-
- // Move the dialog position before 'Show()', not to emerge a ghost!
- gDialog->MoveDialog( *gPref );
-
- // Show the dialog.
- // Setup dialog status, such as of RadioButtons, after 'Show()'.
- gDialog->Show();
- gDialog->SetupDialog( *gPref );
- }
-
-
- // --------------------------------------------------
- // ・ ObeyCommand
- // --------------------------------------------------
- // Respond to commands
-
- Boolean
- CTouchMeApp::ObeyCommand(
- CommandT inCommand,
- void *ioParam)
- {
- Boolean cmdHandled = true;
-
- switch ( inCommand ) {
- case msg_TmeD_ButtonOK:
- case msg_TmeD_ButtonCencel:
- {
- delete gPref;
- delete gDialog;
-
- SendAEQuit();
- }
- break;
-
- case cmd_Save:
- case cmd_SavePrefs:
- {
- // Save all the settings to the preferences file.
- gDialog->InspectDialog( *gPref );
- gPref->SavePrefData();
- }
- break;
-
- case cmd_SavePrefsWFrame:
- {
- // Save the window position only, but not all the other settings.
- Rect theRect;
- gDialog->GetGlobalFrameRect( theRect );
-
- // Revert the settings to read from preferences file, and set the Window position.
- gPref->LoadPrefData();
- gPref->SetWindowRect( theRect );
- gPref->SavePrefData();
- }
- break;
-
- case cmd_Touch:
- {
- FSSpec * theFSSpec = (FSSpec *) ioParam;
- SendAEOpenDoc( *theFSSpec );
- //OpenDocument( theFSSpec );
- }
- break;
-
- case cmd_Close:
- {
- LWindow * theHelpWindow = FindHelpWindow(rPPob_HelpWindow);
-
- if ( theHelpWindow != nil ) delete theHelpWindow;
- }
- break;
-
- case msg_Open_HelpWindow:
- case cmd_HelpMenu:
- {
- // Open the help window. What I should do is just to call 'CreateWindow()'.
- // Any other performance, including a scroll, a resize, a move and others,
- // is the duty of 'LWindow' class object. Thanks to our powerful PowerPlant!
- LWindow * theHelpWindow = FindHelpWindow(rPPob_HelpWindow);
-
- if ( theHelpWindow != nil ) {
- theHelpWindow->Select();
- } else {
- LWindow::CreateWindow(rPPob_HelpWindow, this);
- }
- }
- break;
-
- default:
- {
- cmdHandled = LDocApplication::ObeyCommand( inCommand, ioParam );
- }
- break;
- }
-
- return cmdHandled;
- }
-
-
- // --------------------------------------------------
- // ・ FindHelpWindow
- // --------------------------------------------------
-
- #ifdef COMMENT
-
- // Macintosh Toolbox version (but this time, we do not used it)
- LWindow *
- CTouchMeApp::FindHelpWindow(
- const ResIDT inWindowID )
- {
- WindowPtr theMacWindowP;
- LWindow* theWindowObj;
-
- theMacWindowP = ::FrontWindow();
-
- while ( theMacWindowP != nil ) {
- theWindowObj = LWindow::FetchWindowObject( theMacWindowP );
- if ( theWindowObj != nil && theWindowObj->GetUserCon() == inWindowID ) {
- return theWindowObj;
- }
- theMacWindowP = (WindowPtr) ((WindowPeek) theMacWindowP)->nextWindow;
- }
-
- return (LWindow*) nil;
- }
-
- #else // COMMENT
-
- // Codewarrior PowerPlant version using utility class 'UWindows'
- LWindow *
- CTouchMeApp::FindHelpWindow(
- const ResIDT inWindowID )
- {
- short index = 0;
- WindowPtr theMacWindowP;
- LWindow* theWindowObj;
-
- while ( (theMacWindowP = UWindows::FindNthWindow(++index)) != nil ) {
- theWindowObj = LWindow::FetchWindowObject( theMacWindowP );
- if ( theWindowObj != nil && theWindowObj->GetUserCon() == inWindowID ) {
- return theWindowObj;
- }
- }
-
- return (LWindow*) nil;
- }
-
- #endif // COMMENT
-
-
- // --------------------------------------------------
- // ・ FindCommandStatus
- // --------------------------------------------------
-
- void
- CTouchMeApp::FindCommandStatus(
- CommandT inCommand,
- Boolean &outEnabled,
- Boolean &outUsesMark,
- Char16 &outMark,
- Str255 outName )
- {
- // do nothing else, here.
- switch (inCommand) {
- case cmd_About:
- case cmd_Open:
- case cmd_Close:
- case cmd_Quit:
- outEnabled = true;
- break;
- case cmd_New:
- case cmd_PageSetup:
- outEnabled = false;
- break;
- default:
- LDocApplication::FindCommandStatus( inCommand,
- outEnabled, outUsesMark, outMark, outName );
- break;
- }
- }
-
-
- // --------------------------------------------------
- // ・ UseIdleTime
- // --------------------------------------------------
-
- void
- CTouchMeApp::UseIdleTime(
- const EventRecord & inMacEvent )
- {
- LDocApplication::UseIdleTime( inMacEvent );
-
- // Check the modifier key while opening the documents.
- mKeyModifier = ( (inMacEvent.modifiers & optionKey) != 0 );
- if ( gDialog != nil ) gDialog->Indicator( mKeyModifier );
-
- // Count the items in the document list thrown by an OpenDocument event.
- mCountDoc = 0;
-
- // To quit immediately after the drag&drop operation finished.
- // Do not quit when you have opened the application by a double-clicking.
- if ( ! mOpenApplication && mOpenDocument ) SendAEQuit();
- }
-
-
- // --------------------------------------------------
- // ・ UpdateMenus
- // --------------------------------------------------
-
- void
- CTouchMeApp::UpdateMenus()
- {
- LDocApplication::UpdateMenus();
-
- gHelpMenu->EnableItem();
- }
-
-
- // --------------------------------------------------
- // ・ ShowAboutBox
- // --------------------------------------------------
-
- void
- CTouchMeApp::ShowAboutBox()
- {
- #ifdef PATCH_ACTB
-
- UDesktop::Deactivate();
- ::Alert( ALRT_About, AlertPatchUPP );
- UDesktop::Activate();
-
- #else // PATCH_ACTB
-
- LDocApplication::ShowAboutBox();
-
- #endif // PATCH_ACTB
- }
-
-
- // --------------------------------------------------
- // ・ OpenDocument
- // --------------------------------------------------
- // This procedure is the major part of touchMe program.
- // If option-key is pressed, it works as a 'fetch' command.
- // Otherwise, it works as a 'touch' command regularly.
-
- void
- CTouchMeApp::OpenDocument(
- FSSpec *inMacFSSpec )
- {
- // Setup my program process status.
- mOpenDocument = true;
-
- // Check the modifier key whether option-key is pressed or not.
- // If option-key is pressed, it works as a 'fetch' command and exits immediately.
- if ( mKeyModifier ) {
- if ( gDialog != nil ) gDialog->BroadcastMessage( msg_TmeD_DroppedFile, inMacFSSpec );
- return;
- }
-
- // Count up the number at every execution for each file in the document list.
- mCountDoc ++;
-
- static unsigned long theCrDateTime = 0, theMdDateTime = 0;
-
- // Do special for the first item.
- if ( mCountDoc == 1 ) {
- // If dialog window is open, then read the settings from the dialog.
- if ( gDialog != nil ) gDialog->InspectDialog( *gPref );
-
- // Fetch the date time stamp from the first dropped file.
- UFileInfo::GetFSSpecDateTime( *inMacFSSpec, theCrDateTime, theMdDateTime );
-
- unsigned long theDateTimeSecs;
- ::GetDateTime( &theDateTimeSecs );
-
- // Creation date:
- // Setup the date time stamp according to the flag of settings.
- if ( gPref->GetFlag( touchType_CreationDate, touchFlag_Current ) ) {
- theCrDateTime = theDateTimeSecs;
- } else if ( gPref->GetFlag( touchType_CreationDate, touchFlag_Direct ) ) {
- theCrDateTime = gPref->GetDateTime( touchType_CreationDate );
- } else if ( gPref->GetFlag( touchType_CreationDate, touchFlag_Second ) ) {
- gPref->SetDateTime( touchType_CreationDate, theCrDateTime );
- if ( gDialog != nil ) gDialog->SetupDialog( *gPref );
- }
-
- // Modification date:
- // Setup the date time stamp according to the flag of settings.
- if ( gPref->GetFlag( touchType_ModificationDate, touchFlag_Current ) ) {
- theMdDateTime = theDateTimeSecs;
- } else if ( gPref->GetFlag( touchType_ModificationDate, touchFlag_Direct ) ) {
- theMdDateTime = gPref->GetDateTime( touchType_ModificationDate );
- } else if ( gPref->GetFlag( touchType_ModificationDate, touchFlag_Second ) ) {
- gPref->SetDateTime( touchType_ModificationDate, theMdDateTime );
- if ( gDialog != nil ) gDialog->SetupDialog( *gPref );
- }
- }
-
- // If the{Cr,Md}DateTime has a zero value, then its date time of FSSpec is not changed.
- if ( ! gPref->GetEnabled( touchType_CreationDate ) ) theCrDateTime = 0;
- if ( ! gPref->GetEnabled( touchType_ModificationDate ) ) theMdDateTime = 0;
- UFileInfo::SetFSSpecDateTime( *inMacFSSpec, theCrDateTime, theMdDateTime );
- }
-
-
- // --------------------------------------------------
- // ・ ChooseDocument
- // --------------------------------------------------
-
- void
- CTouchMeApp::ChooseDocument()
- {
- // Deactivate the desktop.
- UDesktop::Deactivate();
-
- // Browse for a document.
- SFTypeList theTypeList;
- StandardFileReply theReply;
- ::StandardGetFile( nil, -1, theTypeList, &theReply );
-
- // Activate the desktop.
- UDesktop::Activate();
-
- // Send an AppleEvent to open the file.
- if ( theReply.sfGood ) SendAEOpenDoc( theReply.sfFile );
- }
-
-
- #ifdef PATCH_ACTB
- // --------------------------------------------------
- // ・ MyAlertPatch
- // --------------------------------------------------
- // Patch for the colored alert dialog. Thanks to Rokkaku Fumio.
-
- #include <UKeyFilters.h>
-
- static pascal Boolean
- MyAlertPatch(
- DialogRef theDialog,
- EventRecord * theEvent,
- short * itemHit )
- {
- Boolean eventOccurred = false;
-
- switch ( theEvent->what ) {
- case keyDown:
- {
- short charCode = (theEvent->message & charCodeMask);
- // if ( (charCode ==kEnterKey ) || (charCode ==kReturnKey) ) {
- if ( UKeyFilters::IsActionKey( charCode ) ) {
- ControlRef aButton;
- Rect aRect;
- short aType;
- long finalTick;
-
- ::GetDialogItem( theDialog, ok, &aType, (Handle *)&aButton, &aRect );
- ::HiliteControl( aButton, kControlButtonPart );
- ::Delay( 8, &finalTick );
- ::HiliteControl( aButton, kControlNoPart );
- *itemHit = kStdOkItemIndex;
- eventOccurred = true;
- }
- }
- break;
-
- case updateEvt:
- {
- WindowRef theWindow = GetDialogWindow( theDialog );
- if ( (WindowRef)theEvent->message == theWindow ) {
- ::SelectWindow( theWindow );
- }
- }
- break;
- }
- return eventOccurred;
- }
- #endif // PATCH_ACTB
-
- // end of program
-